package com.haifeng.asms.auth.handler;

import cn.hutool.core.util.ObjectUtil;
import com.haifeng.asms.commons.common.Rest;
import com.haifeng.asms.commons.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 *  注销成功处理器
 * </p>
 *
 * @author: Haifeng
 * @date: 2020-06-22
 */
@Slf4j
@Component
public class LogOutSuccessHandler implements LogoutSuccessHandler {

    /**
     * 业务描述：注销成功
     *
     * @param request
     * @param response
     * @param authentication
     * @return
     */
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        log.info("退出登录成功:{}", ObjectUtil.isEmpty(authentication)? null : authentication.getPrincipal());

        response.setContentType("application/json;charset=UTF-8");
        Rest rest = Rest.success("退出登录成功");
        response.getWriter().write(JsonUtil.toJsonString(rest));
    }
}
